import pandas as pd


def analyze_titanic_data(df):
    """分析Titanic数据集"""
    if df is None:
        return {}

    analysis_results = {}

    # 基本统计信息
    analysis_results['basic_stats'] = df.describe().round(2).to_dict()

    # 生存率分析
    survival_by_class = df.groupby('Pclass')['Survived'].mean().round(2)
    survival_by_sex = df.groupby('Sex')['Survived'].mean().round(2)
    survival_by_embarked = df.groupby('Embarked')['Survived'].mean().round(2)

    analysis_results['survival'] = {
        'by_class': survival_by_class.to_dict(),
        'by_sex': survival_by_sex.to_dict(),
        'by_embarked': survival_by_embarked.to_dict()
    }

    # 相关性分析
    analysis_results['correlation'] = df.corr()['Survived'].round(2).to_dict()

    return analysis_results